package com.design.patterns.list.prototype;

import com.design.patterns.list.prototype.up.DeepClone;
import com.design.patterns.list.prototype.up.DeepCloneTarget;

/**
 * @author Wangzhiwen
 */
public class Client {

    static void unUse(){
        // 构建
        Sheep sheep = new Sheep("001","red",18);
        // 传统克隆
        Sheep sheep2 = new Sheep(sheep.getName(), sheep.getColor(), sheep.getAge());
        Sheep sheep3 = new Sheep(sheep.getName(), sheep.getColor(), sheep.getAge());
        Sheep sheep4 = new Sheep(sheep.getName(), sheep.getColor(), sheep.getAge());
        Sheep sheep5 = new Sheep(sheep.getName(), sheep.getColor(), sheep.getAge());

        System.out.println(sheep);
        System.out.println(sheep2);
        System.out.println(sheep3);
        System.out.println(sheep4);
        System.out.println(sheep5);
    }

    /**
     * 浅拷贝，对于基本数据类型会完全拷贝，对于引用类型是赋予引用地址值
     * 实现Cloneable接口使用，Object中的Clone方法实现克隆
     */
    static void use() throws CloneNotSupportedException {
        com.design.patterns.list.prototype.up.Sheep sheep = new com.design.patterns.list.prototype.up.Sheep("001", "red", 18);
        com.design.patterns.list.prototype.up.Sheep sheep2 = (com.design.patterns.list.prototype.up.Sheep)sheep.clone();
        com.design.patterns.list.prototype.up.Sheep sheep3 = (com.design.patterns.list.prototype.up.Sheep)sheep.clone();
        com.design.patterns.list.prototype.up.Sheep sheep4 = (com.design.patterns.list.prototype.up.Sheep)sheep.clone();

        System.out.println(sheep);
        System.out.println(sheep2);
        System.out.println(sheep3);
        System.out.println(sheep4);
    }

    /**
     * 深拷贝：1.重写clone方法
     */
    static void deepClone() throws CloneNotSupportedException{
        DeepClone deepClone = new DeepClone("iii","ppp",new DeepCloneTarget("kkk"));
        System.out.println(deepClone.hashCode());
        System.out.println(deepClone);
        System.out.println(deepClone.clone().hashCode());
        System.out.println(deepClone.clone());
        System.out.println(deepClone.deepClone().hashCode());
        System.out.println(deepClone.deepClone());
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        unUse();
        // 浅拷贝
        use();
        // 深拷贝
        deepClone();
    }

}
